﻿using System;
using System.Data;
using DFT.DB;
using DFT.Infos;

namespace DFT.AJAX
{
    public partial class RemoveProfileUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable profilesTable = new DataTable("profiles");
            if (DBManager.GetAllProfilesUser(((UserInfo)Session["userInfo"]).UserId, ref profilesTable))
            {
                if (RemoveTree(profilesTable, Int64.Parse(Request.QueryString["id"])))
                {
                    Response.Write("Success");
                }
                else
                {
                    Response.Write("Failed");
                }
                Response.End();
            }
        }
        
        public Boolean RemoveTree(DataTable profilesTable, Int64 profId)
        {
            if (profId == -1)
            {
                profId = Int64.Parse(profilesTable.Select("parentProfId Is NULL")[0]["profId"].ToString());
            }
            foreach (DataRow childProfile in profilesTable.Select("parentProfId =" + profId))
            {
                if (!RemoveTree(profilesTable, Int64.Parse(childProfile["profId"].ToString())))
                {
                    break;
                }
            }
            return DBManager.RemoveProfileUser(profId);
        }

    }
}